package Config;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;


/**
 * @author Xenophon
 * This class is created when the main program initializes, it will 
 * read a configure file and pass all available parameters to the
 * initialization.
 * The Format of Configure file:
 * --Multiline
 * ---'#'for comments
 * ---every parameter has a line
 * ---Every line has the form: Parameter = value
 * ---Parameters may have no order
 */
public class Configure {
	private static String config_file;
	private static HashMap<String, String> paraPool = new HashMap<String, String>();
	
	public static String BASE_DIR;
	public static String DEST_DIR;
	public static String LOG_PATH;
	public static String LOG_FILE;
	public static String STAT_PATH;
	public static String STAT_FILE;
	public static String DATABASE_DIR;
	public static String GRAPH_DB_DIR;
	public static String ANALYZER_DIR;
	public static String PAJEK_DIR;
	public static String GLOBAL_PROPERTY_FILE_PATH;
	public static int    RAWDATA_THRESHOLD = 100;
	public static boolean STORE_GRAPH_IN_PAJEK = false;
	public static boolean PREPROCESSOR = false;
	public static boolean ANALYZER     = true;
	public static boolean STORE_IN_DB  = false;
	public static boolean STORE_GRAPH_IN_DB = false;
	public static boolean STORE_GLOBAL_PROPERTY = false;
	public static boolean CLUSTER = false;
	
	
	public static void SetConfigureFile(String filePath)
	{
		config_file = filePath;
	}
	
	public static void GetConfigure()throws Exception
	{
		File configfile = new File(config_file);
		if (!configfile.exists())
			throw new Exception("Severe Error, config_file parameter is invalid");
	    BufferedReader bReader = new BufferedReader(new InputStreamReader(new FileInputStream(configfile),"GB2312"));
	    String tmpline;
	    while((tmpline=bReader.readLine())!=null)
	    {
	    	//for comments...
	    	int index = tmpline.indexOf("#");
	    	if(index!=-1)
	    	{
				if (index == 0)
					continue;
				String[] segs = tmpline.split("#");
				tmpline = segs[0];
	    	}
	    	//parse all the parameters...
	    	if(tmpline.indexOf("=")==-1)
	    		continue;
	    	//now analyzing...
	    	String[] segs = tmpline.split("=");
	    	paraPool.put(segs[0].trim(), segs[1].trim());
	    }
	    bReader.close();
	    //now we finished...
	    if(paraPool.containsKey("BASE_DIR"))
	    	BASE_DIR = paraPool.get("BASE_DIR");
	    if(paraPool.containsKey("DEST_DIR"))
	    	DEST_DIR = paraPool.get("DEST_DIR");
	    if(paraPool.containsKey("LOG_PATH"))
	    	LOG_PATH = paraPool.get("LOG_PATH");
	    if(paraPool.containsKey("LOG_FILE"))
	    	LOG_FILE = paraPool.get("LOG_FILE");
	    if(paraPool.containsKey("STAT_PATH"))
	    	STAT_PATH = paraPool.get("STAT_PATH");
		if(paraPool.containsKey("STAT_FILE"))
			STAT_FILE = paraPool.get("STAT_FILE");
		if(paraPool.containsKey("DATABASE_DIR"))
			DATABASE_DIR = paraPool.get("DATABASE_DIR");
		if(paraPool.containsKey("GRAPH_DB_DIR"))
			GRAPH_DB_DIR = paraPool.get("GRAPH_DB_DIR");
		if(paraPool.containsKey("ANALYZER_DIR"))
			ANALYZER_DIR = paraPool.get("ANALYZER_DIR");
		if(paraPool.containsKey("GLOBAL_PROPERTY_FILE_PATH"))
			GLOBAL_PROPERTY_FILE_PATH = paraPool.get("GLOBAL_PROPERTY_FILE_PATH");
		if(paraPool.containsKey("PAJEK_DIR"))
			PAJEK_DIR = paraPool.get("PAJEK_DIR");
		if(paraPool.containsKey("RAWDATA_THRESHOLD"))
			RAWDATA_THRESHOLD = Integer.parseInt(paraPool.get("RAWDATA_THRESHOLD"));
		if(paraPool.containsKey("STORE_GRAPH_IN_DB"))
			STORE_GRAPH_IN_DB = Boolean.parseBoolean(paraPool.get("STORE_GRAPH_IN_DB"));
		if(paraPool.containsKey("PREPROCESSOR"))
			PREPROCESSOR = Boolean.parseBoolean(paraPool.get("PREPROCESSOR"));
		if(paraPool.containsKey("ANALYZER"))
			ANALYZER = Boolean.parseBoolean(paraPool.get("ANALYZER"));
		if(paraPool.containsKey("STORE_IN_DB"))
			STORE_IN_DB = Boolean.parseBoolean(paraPool.get("STORE_IN_DB"));
		if(paraPool.containsKey("STORE_GRAPH_IN_PAJEK"))
			STORE_GRAPH_IN_PAJEK = Boolean.parseBoolean(paraPool.get("STORE_GRAPH_IN_PAJEK"));
		if(paraPool.containsKey("STORE_GLOBAL_PROPERTY"))
			STORE_GLOBAL_PROPERTY = Boolean.parseBoolean(paraPool.get("STORE_GLOBAL_PROPERTY"));
		
	
		if(ANALYZER_DIR==null)
			throw new Exception("Invalid Configure file, No Analyzer dir");
	}
	
	public static void StoreConfigure(BufferedWriter bw) throws IOException
	{
		bw.write("PreProcessor Configure:\n");
		bw.write("BASE_DIR = "+BASE_DIR+"\n");
		bw.write("DEST_DIR = "+DEST_DIR+"\n");
		bw.write("LOG_PATH = "+LOG_PATH+"\n");
		bw.write("LOG_FILE = "+LOG_FILE+"\n");
		bw.write("STAT_PATH = "+STAT_PATH+"\n");
		bw.write("STAT_FILE = "+STAT_FILE+"\n");
		bw.write("ANALYZER_DIR = "+ANALYZER_DIR+"\n");
		bw.write("DATABASE_DIR = "+DATABASE_DIR+"\n");
		bw.write("GRAPH_DB_DIR = "+GRAPH_DB_DIR+"\n");	
		bw.write("GLOBAL_PROPERTY_FILE_PATH = "+GLOBAL_PROPERTY_FILE_PATH+"\n");
		bw.write("PREPROCESSOR = "+Boolean.toString(PREPROCESSOR)+"\n");
		bw.write("ANALYZER = "+Boolean.toString(ANALYZER)+"\n");
		bw.write("STORE_IN_DB = "+Boolean.toString(STORE_IN_DB)+"\n");
		bw.write("STORE_GRAPH_IN_DB = "+Boolean.toString(STORE_GRAPH_IN_DB)+"\n");
		bw.write("STORE_GLOBAL_PROPERTY = "+Boolean.toString(STORE_GLOBAL_PROPERTY)+"\n");
		
	}
}
